/*!
\copyright  Copyright (c) 2020-2021 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief
*/

#include "scan_delegator_role_advertising.h"

#include "pacs_utilities.h"
#include "le_advertising_manager.h"

#include <logging.h>
#include <panic.h>
#include <stdlib.h>
#ifdef USE_SYNERGY
#include "bap_server_lib.h"
#else
#include "bap_server.h"
#endif

static le_adv_item_handle scan_delegator_registered_handle = NULL;
static le_adv_item_data_t scan_delegator_advert;

static bool scanDelegator_GetItemData(le_adv_item_data_t * item)
{
    DEBUG_LOG("scanDelegator_GetAdvertisingDataItem scan_delegator_advert.data:%d", scan_delegator_advert.data);

    PanicNull(item);
    if (scan_delegator_advert.data == NULL)
    {
        leAdvDataItem * broadcast_bap_server_advert;

        broadcast_bap_server_advert = BapServerGetAdvertisingData(BAP_SERVER_BROADCAST_ROLE);
        PanicNull(broadcast_bap_server_advert);

        scan_delegator_advert.data = broadcast_bap_server_advert->data;
        scan_delegator_advert.size = broadcast_bap_server_advert->size;
    }
    *item = scan_delegator_advert;
    return TRUE;
}

static void scanDelegator_ReleaseItemData(void)
{
    if(scan_delegator_advert.data)
    {
        BapServerReleaseAdvertisingItems(BAP_SERVER_BROADCAST_ROLE);
        scan_delegator_advert.data = NULL;
    }
}

#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER

static bool scanDelegator_GetItemInfo(le_adv_item_info_t * info)
{
    PanicNull(info);
    *info = (le_adv_item_info_t){ .placement = le_adv_item_data_placement_advert,
                                    .type = le_adv_type_extended_connectable,
                                    .data_size = BapServerGetAdvertisingDataSize(BAP_SERVER_BROADCAST_ROLE) };
    return TRUE;
}

le_adv_item_callback_t scanDelegator_AdvertisingManagerCallback =
{
    .GetItemData = &scanDelegator_GetItemData,
    .ReleaseItemData = &scanDelegator_ReleaseItemData,
    .GetItemInfo = &scanDelegator_GetItemInfo
};


#else

static bool scanDelegator_CanAdvertise(const le_adv_data_params_t * params)
{
    bool can_advertise = FALSE;
    if(params->data_set == le_adv_data_set_extended_handset
            && params->completeness == le_adv_data_completeness_full
            && params->placement == le_adv_data_placement_advert)
    {
        can_advertise = TRUE;
    }
    return can_advertise;
}

static unsigned int scanDelegator_GetNumberOfAdvertisingItems(const le_adv_data_params_t * params)
{
    if (scanDelegator_CanAdvertise(params))
    {
        return BapServerGetNumberOfAdvertisingItems( BAP_SERVER_BROADCAST_ROLE);
    }
    return 0;
}

static le_adv_data_item_t scanDelegator_GetAdvertisingData(const le_adv_data_params_t * params, unsigned int index)
{
    /* Only one adv data item supported. If more items are needed, this function must be updated */
    PanicFalse(index == 0);
    le_adv_data_item_t item = { .size = 0, .data = NULL };
    if (scanDelegator_CanAdvertise(params))
    {
        scanDelegator_GetItemData(&item);
        return item;
    }

    Panic();
    return item;
}

static void scanDelegator_ReleaseAdvertisingData(const le_adv_data_params_t * params)
{
    UNUSED(params);
    scanDelegator_ReleaseItemData();
}

le_adv_data_callback_t scanDelegator_AdvertisingManagerCallback =
{
    .GetNumberOfItems = &scanDelegator_GetNumberOfAdvertisingItems,
    .GetItem = &scanDelegator_GetAdvertisingData,
    .ReleaseItems = &scanDelegator_ReleaseAdvertisingData
};

#endif

bool LeBapScanDelegator_SetupLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    scan_delegator_registered_handle = LeAdvertisingManager_RegisterAdvertisingItemCallback(NULL, &scanDelegator_AdvertisingManagerCallback);
#else
    scan_delegator_registered_handle = LeAdvertisingManager_Register(NULL, &scanDelegator_AdvertisingManagerCallback);
#endif
    return (scan_delegator_registered_handle ? TRUE : FALSE);
}

bool LeBapScanDelegator_UpdateLeAdvertisingData(void)
{
#ifndef INCLUDE_LEGACY_LE_ADVERTISING_MANAGER
    return (scan_delegator_registered_handle ? LeAdvertisingManager_UpdateAdvertisingItem(scan_delegator_registered_handle) : FALSE);
#else
    return (scan_delegator_registered_handle ? LeAdvertisingManager_NotifyDataChange(NULL, scan_delegator_registered_handle) : FALSE);
#endif
}
